#!/usr/bin/env python3
from middleware import ModelDatabase
import middleware as mid
from pprint import pprint, pformat


class A(mid.Model):
    fields = ["x", "y", "z"]

    def __repr__(self):
        return "A(id={!r}, x={!r}, y={!r}, z={!r}, db={!r})".format(
            self.id, self.x, self.y, self.z, self.manager.id,
        )


class Test(ModelDatabase):
    model = A

    def __init__(self, id, x):
        self.x = x
        super().__init__(id)

    @mid.ordered_by([("x", mid.Order.ASCENDING), ("y", mid.Order.DESCENDING)])
    @mid.all_objects
    def all_objects(self, **kwargs):
        print("[all] kwargs:", pformat(kwargs))
        return [
            A(id=1, x=1, y=3, z=2), A(id=2, x=1, y=2, z=16),
            A(id=3, x=2, y=5, z=3), A(id=4, x=3, y=6, z=0),
            A(id=5, x=3, y=0, z=8),
        ]

    @mid.filtered_by([("z", ">="), ("y", "<=")], multiple=False, always=True)
    @mid.ordered_by([("x", mid.Order.ASCENDING), ("y", mid.Order.DESCENDING)])
    @mid.all_objects
    def with_filters(self, **kwargs):
        print("[filters] kwargs:", pformat(kwargs))
        return [
            A(id=1, x=1, y=3, z=2), A(id=2, x=1, y=2, z=16),
            A(id=3, x=2, y=5, z=3), A(id=4, x=3, y=6, z=0),
            A(id=5, x=3, y=0, z=8),
        ]


db = Test("abc", 3)
db.register()


def test1():
    query = mid.Query(db)
    query.order_dict["x"] = mid.QueryOrderElement(
        field="x", type=mid.Order.ASCENDING,
    )
    query.filter_dict["z"] = mid.QueryFilterElement(
        field="z", operation=">=", value=3,
    )
    query.filter_dict["y"] = mid.QueryFilterElement(
        field="y", operation="<=", value=10,
    )

    best_stats = query.best_method_stats()
    params = query.query_params(best_stats)
    objects = query.execute(best_stats)
    print("\nobjects:", pformat(objects))
    print("\n\n")


def test2():
    print("-----\n\n")
    query = mid.Query(db)
    query.filter_dict["z"] = mid.QueryFilterElement(
        field="z", operation=">=", value=3,
    )
    query.order_dict["x"] = mid.QueryOrderElement(
        field="x", type=mid.Order.ASCENDING,
    )
    query.filter_dict["y"] = mid.QueryFilterElement(
        field="y", operation="<", value=10,
    )

    best_stats = query.best_method_stats()
    params = query.query_params(best_stats)
    objects = query.execute(best_stats)
    print("\nobjects:", pformat(objects))
    print("\n\n")


def test3():
    print("-----\n\n")
    query = mid.Query(db)
    query.filter_dict["y"] = mid.QueryFilterElement(
        field="y", operation="<=", value=10,
    )
    query.filter_dict["z"] = mid.QueryFilterElement(
        field="z", operation=">=", value=3,
    )
    query.order_dict["x"] = mid.QueryOrderElement(
        field="x", type=mid.Order.ASCENDING,
    )

    best_stats = query.best_method_stats()
    params = query.query_params(best_stats)
    objects = query.execute(best_stats)
    print("\nobjects:", pformat(objects))
    print("\n\n")

test1()
test2()
test3()
pprint(db.cache)
